<?php

/**
 * XHTML_Textarea, xhtml_lib
 * 
 * This file contains the source code for the XHTML_Textarea class.
 * @author Fernando Jimenez Mocobono <fjmcool@gmail.com>
 * @version 0.1
 * @package xhtml_lib
 */

include_once "XHTML_All_Attributes_Element.php";
include_once "XHTML__Inline_Forms.php";

/**
 * The XHTML_Textarea class is a handler for the textarea xhtml-strict tag as a part
 * of a xhtml-strict php-handler implementation.
 */
class XHTML_Textarea extends XHTML_All_Attributes_Element implements XHTML__Inline_Forms {
    
    // xhtml attributes
    
    /**
     * Unique name for the textarea element.
     *
     * @var string
     */
    protected $name;
    
    /**
     * Number of rows visible in the text-area.
     *
     * @var integer
     */
    protected $rows;
    
    /**
     * Number of columns visible in the text-area.
     *
     * @var integer
     */
    protected $cols;
    
    /**
     * Disables the textarea element when it first loads so that the user can not 
     * write text in it, or select it. Cannot be used with type="hidden".
     * 
     * @var boolean
     */
    protected $disabled;
    
    /**
     * Indicates that the value of this field cannot be modified. Only used 
     * with type="text".
     *
     * @var boolean
     */
    protected $readOnly;
    
    /**
     * Script for the onselect event.
     *
     * @var string
     */
    protected $onSelectScript;
    
    /**
     * Script for the onchange event.
     *
     * @var string
     */
    protected $onChangeScript;
    
    // Own elements
    /**
     * Inner text.
     * 
     * @var string
     */
    protected $text;
    
    
    // Methods
    
    /**
     * Constructs a XHTML_Textarea object with its required cols and rows attributes.
     * 
     * @param integer $cols
     * @param integer $rows
     */
    function __construct($cols, $rows)
    {
        $this->cols = $cols;
        $this->rows = $rows;
        settype($this->elements, "array");
    }
            
    /**
     * Sets the unique name for the textarea element.
     *
     * @param string $name
     */
    function setName($name)
    {
        $this->name = $name;
    }
    
    /**
     * Gets the unique name for the textarea element.
     *
     * @return string
     */
    function getName()
    {
        return $this->name;
    }
    
    /**
     * Sets the number of rows visible in the text-area.
     *
     * @param integer $rows
     */
    function setRows($rows)
    {
        $this->rows = $rows;
    }
    
    /**
     * Gets the number of rows visible in the text-area.
     *
     * @return integer
     */
    function getRows()
    {
        return $this->rows;
    }
    
    /**
     * Sets the number of columns visible in the text-area.
     * 
     * @param integer $cols
     */
    function setCols($cols)
    {
        $this->cols = $cols;
    }
    
    /**
     * Gets the number of columns visible in the text-area.
     * 
     * @return integer
     */
    function getCols()
    {
        return $this->cols;
    }
    
    /**
     * Indicates if the the textarea element should be disabled when it first 
     * loads so that the user can not write text in it, or select it.
     * 
     * @param boolean $disabled
     */
    function setDisabled($disabled)
    {
        $this->disabled = $disabled;
    }
    
    /**
     * Checks if the the textarea element should be disabled when it first 
     * loads so that the user can not write text in it, or select it.
     *
     * @return boolean
     */
    function isDisabled()
    {
        return $this->disabled;
    }
    
    /**
     * Indicates if the value of this field cannot be modified.
     *
     * @param boolean $readOnly
     */
    function setReadOnly($readOnly)
    {
        $this->readOnly = $readOnly;
    }
    
    /**
     * Checks if the value of this field cannot be modified.
     * 
     * @return boolean
     */
    function isReadOnly()
    {
        return $this->readOnly;
    }
    
    /**
     * Sets the script for the onselect event.
     * 
     * @param string $script
     */
    function setOnSelect($script)
    {
        $this->onSelectScript = $script;
    }
    
    /**
     * Gets the script for the onselect event.
     * 
     * @return string
     */
    function getOnSelect()
    {
        return $this->onSelectScript;
    }
    
    /**
     * Sets the script for the onchange event.
     * 
     * @param string $script
     */
    function setOnChange($script)
    {
        $this->onChangeScript = $script;
    }
    
    /**
     * Sets the inner text of this element.
     *
     * @param string $text
     */
    function setText($text)
    {
        $this->text = $text;
    }
    
    /**
     * Gets the inner text of this element.
     * 
     * @return string
     */
    function getText()
    {
        return $this->text;
    }
    
    /**
     * Gets the script for the onchange event.
     * 
     * @return string
     */
    function getOnChange()
    {
        return $this->onChangeScript;
    }
    

    // Overriding the toString method for a proper implementation.
    
    /**
    * Returns the string representation for echoing the class atributes.
    *
    * @return string
    */
    function toString()
    {
        $auxString = "<textarea".parent::toString();
        
        /*
         * Additions of the object own attributes to the return string.
         */
        
        if (isset($this->name)) {
            $auxString = $auxString."name=\"".$this->name."\" ";
        }
        
        $auxString = $auxString."rows=\"".$this->rows."\" ";
        
        $auxString = $auxString."cols=\"".$this->cols."\" ";
        
        if ($this->disabled) {
            $auxString = $auxString."disabled=\"disabled\" ";
        }
        
        if ($this->readOnly) {
            $auxString = $auxString."readonly=\"readonly\" ";
        }
        
        if (isset($this->onSelectScript)) {
            $auxString = $auxString."onselect=\"".$this->onSelectScript."\" ";
        }
        
        if (isset($this->onChangeScript)) {
            $auxString = $auxString."onchange=\"".$this->onChangeScript."\" ";
        }
        
        $auxString = $auxString.">";
        
        $auxString = $auxString.$this->text."";
        
        $auxString = $auxString."</textarea>\n";
        
        return $auxString;
    }
    
}

?>